{
 "cells": [
  {
   "cell_type": "raw",
   "metadata": {
    "raw_mimetype": "text/restructuredtext"
   },
   "source": [
    ".. _nb_constraints_eps:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "%%capture\n",
    "%run ./index.ipynb"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### $\\epsilon$-Constraint Handling"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Instead of directly redefining the problem, one can also redefine an algorithm that changes its conclusion, whether a solution is feasible given the constraint violation over time. One common way is to allow an $\\epsilon$ amount of infeasibility to still consider a solution as feasible. Now, one can decrease the $\\epsilon$ over time and thus finally fall back to a feasibility first algorithm. The $\\epsilon$  has reached zero depending on `perc_eps_until`. For example,  if `perc_eps_until=0.5` then after 50\\% of the run has been completed $\\epsilon=0$."
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {
    "raw_mimetype": "text/restructuredtext"
   },
   "source": [
    ".. admonition:: Info\n",
    "    :class: myOwnStyle\n",
    "\n",
    "    This constraint handling method has been added recently and is still experimental. Please let us know if it has or has not worked for your problem."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Such a method can be especially useful for equality constraints which are difficult to satisfy. See the example below:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "from pymoo.algorithms.soo.nonconvex.de import DE\n",
    "from pymoo.constraints.eps import AdaptiveEpsilonConstraintHandling\n",
    "from pymoo.optimize import minimize\n",
    "from pymoo.problems.single import G1\n",
    "\n",
    "problem = ConstrainedProblemWithEquality()\n",
    "\n",
    "algorithm = AdaptiveEpsilonConstraintHandling(DE(), perc_eps_until=0.5)\n",
    "\n",
    "res = minimize(problem,\n",
    "               algorithm,\n",
    "               ('n_gen', 200),\n",
    "               seed=1,\n",
    "               verbose=False)\n",
    "\n",
    "print(\"Best solution found: \\nX = %s\\nF = %s\\nCV = %s\" % (res.X, res.F, res.CV))"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
